<?php
// 消息管理

namespace app\console\controller;

use think\Loader;
use think\Url;
use think\Request;
use think\Controller;
use app\console\model\MemberMessage as MemberMessageModel;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Cache;

class MemberMessageM extends Controller
{

    /**
     * 系统消息管理
     */
    public function index(Request $request, MemberMessageModel $memberMessage)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
//        $condition['id'] = ['gt', 0];
//        $condition['member_id'] = ['eq', 0];

         $condition['message_type'] = 3;

        // 筛选
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];



        // 读取 会员信息
        $data = $memberMessage->where($condition)->order(['create_time' => 'desc'])->paginate(10, false, ['query' => $inquire])->each(function ($item,$key){
            $item->content =  strip_tags(str_replace('&nbsp;','',$item['content']));
        });


        return $this->fetch('index', [
            'mate_title' => '消息列表',
            'data'       => $data,

        ]);
    }


    /**
     * 系统消息新增
     */
    public function create(Request $request, MemberMessageModel $memberMessage)
    {
        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            $validate = Loader::validate('MemberMessage');
            if (!$validate->check($data)) fail($validate->getError());

            // 提交数据

            $dispose = $memberMessage->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('MemberMessageM/index'));
            }

            fail('操作失败');

        }
        $member_list = db('member')->where('username','neq','')->select();
        return $this->fetch('create', [
            "mate_title" => '新增消息',
            'member_list' => $member_list,
            'data'       => ClassifyAlias::dimension(Cache::get('goodsType'))
        ]);
    }


    /**
     * 系统消息编辑
     */
    public function update($id, Request $request, MemberMessageModel $memberMessage)
    {
        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 提交数据
            $dispose = $memberMessage->allowField(true)->save($data, ['id' => $id]);

            if ($dispose !== false) {
                win('操作成功', Url::build('MemberMessageM/index'));
            }

            fail('操作失败');

        }

        $item = MemberMessageModel::get($id);

        return $this->fetch('create', [
            "mate_title" => '编辑消息',
            'item'       => $item,
            'member_list' => db('member')->where('username','neq','')->select()

        ]);
    }


    /**
     * 系统消息删除
     */
    public function destroy($id, Request $request, MemberMessageModel $memberMessage)
    {
        if ($memberMessage->destroy($id)) {

            win('删除成功', Url::build('MemberMessageM/index'));
        }

        fail('删除失败');
    }
}